import { _decorator, Color, Component, EventTouch, math, Node, Prefab, Sprite, tween, v2, v3, Vec2, Vec3 } from 'cc';
import { PoolManager } from './PoolManager';
import { CycleItem } from './CycleItem';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('CycleItemManager')
export class CycleItemManager extends Component {
    public static Instance: CycleItemManager = null;

    @property(Prefab)
    ItemPrefab: Prefab = null;

    @property(Sprite)
    Icon1: Sprite = null;

    @property(Sprite)
    Icon2: Sprite = null;

    @property(Node)
    LeftUp: Node = null;

    @property(Node)
    RightDown: Node = null;

    @property(Node)
    Destroy: Node = null;

    @property
    MoveSpeed: number = 0.5;

    @property
    BallSpeed: number = 0.5;

    @property
    Gravity: number = -3;

    @property(Node)
    AredNode: Node = null;

    @property(Node)
    StartNode: Node = null;

    @property
    MaxCount: number = 200;

    private _plan: number = 0.5;
    private _leftUpPos: Vec2 = new Vec2();
    private _rightDownPos: Vec2 = new Vec2();
    private _ballRadius: number = 15;
    private _count: number = 0;
    private _length: number = 0;
    private _desY: number = 0
    private _isPause: boolean = false;
    private _curCount: number = 0;

    protected onLoad(): void {
        CycleItemManager.Instance = this;
        PoolManager.Instance.preload(this.ItemPrefab, 1000);

        this._leftUpPos = v2(this.LeftUp.getWorldPosition().x, this.LeftUp.getWorldPosition().y);
        this._rightDownPos = v2(this.RightDown.getWorldPosition().x, this.RightDown.getWorldPosition().y);
        this._desY = this.Destroy.getWorldPosition().y;

        this.AredNode.once(Node.EventType.TOUCH_START, this.onTouchStart, this);
        tween(this.StartNode)
            .to(1, { scale: v3(1.2, 1.2, 1.2) }, { easing: `sineIn` })
            .to(1, { scale: v3(1, 1, 1) }, { easing: `sineIn` })
            .union()
            .repeatForever()
            .start();
    }

    protected start(): void {
        this._length = this._rightDownPos.x - this._leftUpPos.x;
        // this.addItems();
    }

    protected update(dt: number): void {
        if (this._isPause) return;
        this._plan += dt * this.MoveSpeed;
        if (this._plan > 1) {
            this._plan = 1;
            this.MoveSpeed *= -1;
        } else if (this._plan < 0) {
            this._plan = 0;
            this.MoveSpeed *= -1;
        }
        this.move();

        if (this._count > 0) {
            this._count--;
            this.createItem();
        }
    }

    addItems(count: number = 1) {
        this._count += count;
    }

    createItem() {
        if (this._curCount > this.MaxCount) return;
        this._curCount++;
        console.error(this._curCount);

        const node: Node = PoolManager.Instance.get(this.ItemPrefab);
        const x: number = math.randomRange(this._leftUpPos.x + this._ballRadius + 20, this._rightDownPos.x - this._ballRadius - 20);
        const y: number = math.randomRange(this._rightDownPos.y + 300, this._leftUpPos.y - 100);
        const worldPos: Vec3 = new Vec3(x, y, 0);
        node.getComponent(CycleItem).init(this.node, worldPos, this.BallSpeed, this.Gravity, this._rightDownPos.y + this._ballRadius, this._desY, this._leftUpPos.y - this._ballRadius);
    }

    removeItem(target: Node) {
        PoolManager.Instance.put(target);
        this._curCount--;
    }

    initItem(pos: Vec3) {
        this._curCount++;
        const node: Node = PoolManager.Instance.get(this.ItemPrefab);
        node.getComponent(CycleItem).init(this.node, pos, this.BallSpeed, this.Gravity, this._rightDownPos.y + this._ballRadius, this._desY, this._leftUpPos.y - this._ballRadius);
    }

    checkPos(x: number): boolean {
        // console.error(this._plan, this._leftUpPos.x + 0.7 * this._plan * this._length, x, this._leftUpPos.x + 0.7 * this._plan * this._length + 0.3 * this._length);
        return x > (this._leftUpPos.x + 0.7 * this._plan * this._length) && x < (this._leftUpPos.x + 0.7 * this._plan * this._length + 0.3 * this._length);
    }


    private move() {
        this.Icon1.fillRange = this._plan * 0.7;
        this.Icon2.fillRange = this._plan * 0.7 - 0.7;
    }

    private pause() {
        this._isPause = !this._isPause;
    }

    onTouchStart(event: EventTouch) {
        const pos: Vec2 = event.getUILocation();
        this.initItem(v3(pos.x, pos.y, 0))
        this.AredNode.active = false;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }
}


